﻿package mortal.game.view.shop
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import __AS3__.vec.*;
    import com.greensock.layout.*;
    import com.mui.controls.*;
    import com.mui.events.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.ui.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.npc.data.*;
    import mortal.game.view.shopMall.data.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class ShopModule extends BaseWindow
    {
        private var _shopTabData:Array;
        private var _shopCode:int;
        private var _npcId:int;
        private var _showIndex:int = 10;
        private var _tabBar:GTabarNew;
        private var _pageSelecter:PageSelecter;
        private var _shopItemPanel:GTileList;
        private var _buyBackPanel:GTileList;
        private var _btnBuy:GButton;
        private var _btnSell:GButton;
        private var _moneyBox:MoneyBox;
        private var _tshop:TShop;
        private var _items:Vector.<TShopSell>;

        public function ShopModule(param1:ILayer = null)
        {
            this._shopTabData = new Array();
            this._items = new Vector.<TShopSell>;
            super(param1);
            this.setSize(335, 500);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.mouseEnabled = true;
            this.pushUIToDisposeVec(UIFactory.bg(15, 62, 312, 310, this, ImagesConst.PanelBg));
            this.pushUIToDisposeVec(UIFactory.bg(15, 385, 312, 72, this, ImagesConst.PanelBg));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(30080), 22, 387, 220, 20, this, null, true));
            this._tabBar = UIFactory.gTabBarNew(2, 32, this._shopTabData, 335, 500, 65, 26, this, this.tabBarChangeHandler, "TabButtonNew");
            this._shopItemPanel = UIFactory.tileList(21, 70, 300, 270, this);
            this._shopItemPanel.columnWidth = 145;
            this._shopItemPanel.rowHeight = 50;
            this._shopItemPanel.horizontalGap = 5;
            this._shopItemPanel.verticalGap = 5;
            this._shopItemPanel.setStyle("cellRenderer", ShopCellRenderer);
            this._buyBackPanel = UIFactory.tileList(24, 405, 310, 48, this);
            this._buyBackPanel.columnWidth = 46;
            this._buyBackPanel.rowHeight = 46;
            this._buyBackPanel.horizontalGap = 2;
            this._buyBackPanel.verticalGap = 2;
            this._buyBackPanel.setStyle("cellRenderer", BuyBackCellRenderer);
            var _loc_1:* = GlobalStyle.textFormatBai;
            _loc_1.align = AlignMode.CENTER;
            this._pageSelecter = UIFactory.pageSelecter(100, 345, this, PageSelecter.CompleteMode);
            this._pageSelecter.setbgStlye(ImagesConst.ComboBg, _loc_1);
            this._pageSelecter.pageTextBoxSize = 50;
            this._pageSelecter.configEventListener(Event.CHANGE, this.onPageChange);
            this._btnBuy = UIFactory.gButton(Language.getString(30068), 227, 465, 41, 22, this);
            this._btnSell = UIFactory.gButton(Language.getString(30003), 277, 465, 41, 22, this);
            this._moneyBox = UICompomentPool.getUICompoment(MoneyBox);
            this._moneyBox.autoUpdate = true;
            this._moneyBox.x = 14;
            this._moneyBox.y = 466;
            this.addChild(this._moneyBox);
            this.configEventListener(MouseEvent.CLICK, this.btnClickHandler);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._tabBar.dispose(param1);
            this._shopItemPanel.dispose(param1);
            this._pageSelecter.dispose(param1);
            this._btnBuy.dispose(param1);
            this._btnSell.dispose(param1);
            this._buyBackPanel.dispose(param1);
            this._moneyBox.dispose(param1);
            this._tabBar = null;
            this._shopItemPanel = null;
            this._pageSelecter = null;
            this._btnBuy = null;
            this._btnSell = null;
            this._buyBackPanel = null;
            this._moneyBox = null;
            this._tshop = null;
            this._items.length = 0;
            this._shopTabData.length = 0;
            this.removeSpecialListener();
            this._shopCode = 0;
            super.disposeImpl(param1);
            return;
        }// end function

        private function onPageChange(event:Event = null) : void
        {
            this._shopItemPanel.dataProvider = this.getShopDataProvider();
            return;
        }// end function

        private function tabBarChangeHandler(event:MuiEvent = null) : void
        {
            this._shopCode = this._shopTabData[this._tabBar.selectedIndex].name;
            this._items = ShopConfig.instance.getShopSellItemByShopCode(this._shopCode);
            this._tshop = ShopConfig.instance.getTShopByCode(this._shopCode);
            this._pageSelecter.maxPage = Math.ceil(this._items.length / this._showIndex);
            this._pageSelecter.currentPage = 1;
            this.title = this._tshop.name;
            this._moneyBox.setMoney(this._tshop.unit);
            this.onPageChange();
            return;
        }// end function

        private function getShopDataProvider() : DataProvider
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:ShopItemData = null;
            var _loc_6:Object = null;
            var _loc_1:* = new DataProvider();
            if (this._items && this._items.length)
            {
                _loc_2 = this._showIndex * (this._pageSelecter.currentPage - 1);
                if (this._showIndex * this._pageSelecter.currentPage <= this._items.length)
                {
                    _loc_3 = this._showIndex * this._pageSelecter.currentPage;
                }
                else
                {
                    _loc_3 = this._items.length;
                }
                _loc_4 = _loc_2;
                while (_loc_4 < _loc_3)
                {
                    
                    _loc_5 = new ShopItemData(this._items[_loc_4]);
                    _loc_6 = {data:_loc_5, npcId:this._npcId};
                    _loc_1.addItem(_loc_6);
                    _loc_4++;
                }
            }
            return _loc_1;
        }// end function

        public function updateTabBar(param1) : void
        {
            if (param1 is NpcFunctionData)
            {
                this.shopCode = param1.value;
                this._npcId = param1.npcId;
            }
            else if (param1 is int)
            {
                this.shopCode = param1;
            }
            this._shopTabData = ShopConfig.instance.getShopTabByCode(this._shopCode);
            this._tabBar.dataProvider = this._shopTabData;
            this.tabBarChangeHandler();
            return;
        }// end function

        public function set shopCode(param1:int) : void
        {
            this.removeSpecialListener();
            this._shopCode = param1;
            this.addSpecialListener();
            return;
        }// end function

        private function updateItems(param1:Object = null) : void
        {
            this._items = ShopConfig.instance.getShopSellItemByShopCode(this._shopCode);
            this._tshop = ShopConfig.instance.getTShopByCode(this._shopCode);
            this._pageSelecter.maxPage = Math.ceil(this._items.length / this._showIndex);
            this._pageSelecter.currentPage = 1;
            this.onPageChange();
            return;
        }// end function

        private function addSpecialListener() : void
        {
            if (this._shopCode == 3008)
            {
                NetDispatcher.addCmdListener(ServerCommand.RuneLevelUp, this.updateItems);
                NetDispatcher.addCmdListener(ServerCommand.RuneAdd, this.updateItems);
            }
            return;
        }// end function

        private function removeSpecialListener() : void
        {
            if (this._shopCode == 3008)
            {
                NetDispatcher.removeCmdListener(ServerCommand.RuneLevelUp, this.updateItems);
                NetDispatcher.removeCmdListener(ServerCommand.RuneAdd, this.updateItems);
            }
            return;
        }// end function

        public function get shopCode() : int
        {
            return this._shopCode;
        }// end function

        public function updateBuyBackList() : void
        {
            var _loc_3:SBuyBackItem = null;
            var _loc_5:Object = null;
            var _loc_1:* = Cache.instance.shop.buyBackList;
            var _loc_2:* = new DataProvider();
            var _loc_4:int = 0;
            while (_loc_4 < 6)
            {
                
                _loc_3 = _loc_1[_loc_4];
                _loc_5 = {data:_loc_3};
                _loc_2.addItem(_loc_5);
                _loc_4++;
            }
            this._buyBackPanel.dataProvider = _loc_2;
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            return;
        }// end function

        override public function hide() : void
        {
            CursorManager.currentCurSorType = CursorManager.Arrow;
            CursorManager.showCursor(CursorManager.Arrow);
            super.hide();
            return;
        }// end function

        private function btnClickHandler(event:MouseEvent) : void
        {
            var _loc_2:* = event.target as GButton;
            switch(_loc_2)
            {
                case this._btnBuy:
                {
                    CursorManager.currentCurSorType = CursorManager.BUY;
                    CursorManager.showCursor(CursorManager.currentCurSorType);
                    break;
                }
                case this._btnSell:
                {
                    CursorManager.currentCurSorType = CursorManager.SELL;
                    CursorManager.showCursor(CursorManager.currentCurSorType);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

    }
}
